package com.laiketui.order.mapstruct;

import com.laiketui.core.utils.help.SpringHelper;
import com.laiketui.order.api.dto.OrderParentMessageDTO;
import com.laiketui.order.api.dto.StoreOrderDTO;
import com.laiketui.order.api.dto.WarehouseDTO;
import com.laiketui.order.api.params.AgainPlaceOrderParams;
import com.laiketui.order.api.params.OrderInfoParams;
import com.laiketui.order.api.params.PlaceOrderParams;
import com.laiketui.order.api.params.UserPlaceOrderParams;
import com.laiketui.order.domain.OrderOfflineExamine;
import com.laiketui.order.domain.OrderParentMessage;
import com.laiketui.order.entity.StoreOrderEntity;
import com.laiketui.order.service.ExamineOrderService;
import com.laiketui.order.service.WarehouseService;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Mapper
public interface StoreOrderMapstructMapper {

    StoreOrderMapstructMapper INSTANCE = Mappers.getMapper(StoreOrderMapstructMapper.class);


    @Mapping( target = "taxation",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTaxation()))")
    @Mapping( target = "payPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getPayPrice()))")
    @Mapping( target = "settlementAmount",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getSettlementAmount()))")
    @Mapping( target = "totalCostPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTotalCostPrice()))")
    @Mapping( target = "opTotalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getOpTotalPrice()))")
    @Mapping( target = "opPayPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getOpPayPrice()))")
    @Mapping( target = "opTaxation",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getOpTaxation()))")
    @Mapping( target = "deductionAmount",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getDeductionAmount()))")
    @Mapping( target = "totalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTotalPrice()))")
    @Mapping( target = "oldTotalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getOldTotalPrice()))")
    StoreOrderDTO toDTO(StoreOrderEntity order);


    default StoreOrderDTO convert(StoreOrderEntity entity){
        StoreOrderDTO dto = this.toDTO(entity);
        WarehouseDTO warehouseDTO = new WarehouseDTO();
        warehouseDTO.setProvice(entity.getProvice());
        warehouseDTO.setCity(entity.getCity());
        warehouseDTO.setArea(entity.getArea());
        warehouseDTO.setName(entity.getName());
        warehouseDTO.setAddress(entity.getAddress());
        warehouseDTO.setMobile(entity.getMobile());
        WarehouseService beanInfo = SpringHelper.getBeanInfo(WarehouseService.class);
        dto.setChannel(beanInfo.getChannel());
        dto.setDeliveryAddress(warehouseDTO);

        WarehouseDTO factoryAddress = new WarehouseDTO();
        if(null != entity.getUSignatoryType() && entity.getUSignatoryType() == 1){
            factoryAddress.setName(entity.getUUserName());
        }else{
            factoryAddress.setName(entity.getUCompanyName());
        }
        factoryAddress.setMobile(entity.getUMobile());
        factoryAddress.setProvice(entity.getUProvince());
        factoryAddress.setCity(entity.getUCity());
        factoryAddress.setArea(entity.getUCounty());
        factoryAddress.setAddress(entity.getUDetailedAddress());
        dto.setFactoryAddress(factoryAddress);

        ExamineOrderService examineOrderService = SpringHelper.getBeanInfo(ExamineOrderService.class);
        OrderInfoParams params = new OrderInfoParams();
        params.setOrderParentNo(entity.getOrderParentNo());
        OrderOfflineExamine offlineExamine = examineOrderService.passOrderInfo(params);
        if(offlineExamine != null){
            dto.setExamineTime(offlineExamine.getExamineTime());
        }

        return dto;
    }
    default List<StoreOrderDTO> toDTOs(List<StoreOrderEntity> entities){
        if(CollectionUtils.isEmpty(entities)){
            return new ArrayList<>();
        }
        WarehouseService beanInfo = SpringHelper.getBeanInfo(WarehouseService.class);
        Map<String,String> orderNoFactoryNameMap= beanInfo.getFactoryNameMap(entities);
        return entities.stream().map(v->{
            StoreOrderDTO convert = this.convert(v);
            convert.setFactoryName(orderNoFactoryNameMap.get(v.getOrderNo()));
            convert.setChannel(beanInfo.getChannel());
            return convert;
        }).collect(Collectors.toList());
    };

}